package com.wanqing.community.manager.impl;

import com.wanqing.community.bean.GoodsBean;
import com.wanqing.community.dao.GoodsDao;
import com.wanqing.community.manager.GoodsManager;
import com.wanqing.community.util.CommunityException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/2/14.
 */
@Service("goodsManager")
public class GoodsManagerImpl implements GoodsManager {
    @Resource
    private GoodsDao goodsDao;

    private static Logger log = Logger.getLogger(GoodsManagerImpl.class);

    @Override
    public int getGoodsCount(Map<String, Object> mapPram)  throws CommunityException {
        int total = 0;
        try {
            total = this.goodsDao.getGoodsCount(mapPram);
        } catch (Exception e) {
            log.error(e.toString());
            throw new CommunityException(e.getMessage());
        }
        return total;
    }

    @Override
    public List<GoodsBean> getGoods(Map<String, Object> mapPram)  throws CommunityException{
        try {
            List<GoodsBean> list = null;
            list = goodsDao.getGoods(mapPram);
            return list;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CommunityException(e.getMessage());
        }
    }

    @Override
    public boolean addGood(GoodsBean goodsBean)  throws CommunityException{
        int total = 0;
        try {
            total = goodsDao.addGood(goodsBean);
        } catch (Exception e) {
            log.error("添加产品时出现错误!" + e.toString());
            throw new CommunityException(e.getMessage());
        }
        return total > 0 ? true : false;
    }

    @Override
    public boolean updateGood(GoodsBean goodsBean)  throws CommunityException{
        int total = 0;
        try {
            total = goodsDao.updateGood(goodsBean);
        } catch (Exception e) {
            log.error("添加产品时出现错误!" + e.toString());
            throw new CommunityException(e.getMessage());
        }
        return total > 0 ? true : false;
    }

    @Override
    public boolean deleteGoods(List<String> list)  throws CommunityException{
        try {
            int count = goodsDao.deleteGoods(list);
            return count > 0;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CommunityException(e.getMessage());
        }
    }

    @Override
    public GoodsBean findGoodsById(String id)  throws CommunityException{
        GoodsBean goodsBean = new GoodsBean();
        try {
            goodsBean = goodsDao.findGoodsById(id);
        } catch (Exception e) {
            log.error("根据ID查找行业异常！" + e.toString());
            throw new CommunityException(e.getMessage());
        }
        return goodsBean;
    }
}
